package com.lhj.springboot.web.controller;

import com.lhj.springboot.web.pojo.Person;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Description
 * @Author lhj
 * @Date 2024/6/15 10:58
 */
@Slf4j
@RestController
public class HelloController {

    /**
     * Ant 路径匹配规则
     * * 匹配一个或多个字符
     * ? 匹配任意一个字符
     * {p1:xxx} 匹配路径参数 并指定路径名称p1
     * [a-f] 匹配字符范围
     * @param path
     * @return
     */
    @GetMapping("/a*/b?/{p1:[a-f]+}")
    public String hello(@PathVariable(value = "p1") String path) {
        log.info("路径变量：{}", path);
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(requestAttributes, "The servlet request attributes be not null");
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getRequestURI();
    }
    
    @GetMapping("/person")
    public Person person() {
        Person person = new Person();
        person.setId(1L);
        person.setUserName("lhj");
        person.setEmail("");
        person.setAge(18);
        return person;
    } 
    
}
